package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.R;
import com.itheima.reggie.contants.MessageConstant;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.ex.CustomerException;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 分类管理实现
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;


    /**
     * 新增分类
     * @param category
     * @return
     */
    @Override
    public R addCategory(Category category) {
        // 校验数据库中是否已存在这个分类
        Category dbCategory = this.getOne(Wrappers.<Category>lambdaQuery().eq(Category::getName, category.getName()));

        if (!Objects.isNull(dbCategory)) {
            throw new CustomerException(MessageConstant.CATEGORY_NAME_EXISTE);
        }

        // 新增分类
        this.save(category);

        return R.success(MessageConstant.CATEGORY_ADD_SUCCESS);
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public R categoryPage(Integer page, Integer pageSize) {
        Page<Category> iPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Category> lqw = Wrappers.<Category>lambdaQuery().orderByAsc(Category::getSort);
        this.page(iPage, lqw);

        return R.success(iPage);
    }

    /**
     * 删除分类
     * @param id
     * @return
     */
    @Override
    public R deleteById(Long id) {

        int count = dishService.count(Wrappers.<Dish>lambdaQuery().eq(Dish::getId, id));
        if (count > 0) {
            throw new CustomerException("当前分类下有菜品数据，不能删除!!");
        }
        count = setmealService.count(Wrappers.<Setmeal>lambdaQuery().eq(Setmeal::getId, id));
        if (count > 0) {
            throw new CustomerException("当前分类下有套餐数据，不能删除!!");
        }

        // 可以删除
        this.removeById(id);

        return R.success("删除分类成功");
    }

    /**
     * 修改分类
     * @param category
     * @return
     */
    @Override
    public R modify(Category category) {
        Category dbCategory = this.getOne(Wrappers.<Category>lambdaQuery().eq(Category::getName, category.getName()));
        if (dbCategory == null || dbCategory.getId() == category.getId()){
            this.updateById(category);
        }else {
            throw new CustomerException("分类名称已存在，请更换！！");
        }

        return R.success("修改分类成功");
    }

    @Override
    public R findList(Category category) {
        LambdaQueryWrapper<Category> lqw = Wrappers.<Category>lambdaQuery().eq(category.getType()!= null,Category::getType, category.getType());
        lqw.orderByAsc(Category::getSort);

        List<Category> list = this.list(lqw);

        return R.success(list);
    }
}